package coml.jiyun.hp.jiaohuafei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import coml.jiyun.hp.jiaohuafei.R;
import coml.jiyun.hp.jiaohuafei.adapter.ExchangeAdapter;
import coml.jiyun.hp.jiaohuafei.bean.ExchangeBean;
import coml.jiyun.hp.jiaohuafei.iview.IView;
import coml.jiyun.hp.jiaohuafei.presenter.Presenter;

public class ExchangeActivity extends AppCompatActivity implements View.OnClickListener , IView {

    private static final String TAG = "ExchangeActivity";
    private TextView et;
    private EditText cell_phone;
    private EditText cell_phone_affirm;
    private TextView tv_exchange;
    private RecyclerView rv;
    private Button cancel;
    private Button affirm;
    private ExchangeAdapter exchangeAdapter;
    private List<ExchangeBean.DataBean.ListBean> list;
    private Intent intent;
    private String name;
    private int i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange);
        initView();
        Presenter presenter = new Presenter(this);
        presenter.getData();
    }

    private void initView() {
        et = (TextView) findViewById(R.id.et);
        cell_phone = (EditText) findViewById(R.id.cell_phone);
        cell_phone_affirm = (EditText) findViewById(R.id.cell_phone_affirm);
        tv_exchange = (TextView) findViewById(R.id.tv_exchange);
        rv = (RecyclerView) findViewById(R.id.rv);
        cancel = (Button) findViewById(R.id.cancel);
        affirm = (Button) findViewById(R.id.affirm);

        cancel.setOnClickListener(this);
        affirm.setOnClickListener(this);

        intent = getIntent();
        name = intent.getStringExtra("name");
        tv_exchange.setText(name);
        i = Integer.parseInt(name);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));
        exchangeAdapter = new ExchangeAdapter(this);
        rv.setAdapter(exchangeAdapter);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel://取消
                finish();
                break;
            case R.id.affirm://确认d
                initJudge();
                break;
        }
    }

    private void initJudge() {
        String cancels = cell_phone.getText().toString();
        String cell_phones = cell_phone_affirm.getText().toString();
        if (!cancels.isEmpty() || !cell_phones.isEmpty()){
            if (cancels.length()==11 && cell_phones.length() == 11){
                if (cancels.equals(cell_phones)){
                    int selectposition = exchangeAdapter.asd;
                    if (selectposition == -1){
                        Toast.makeText(this, "请选择一个兑换项目", Toast.LENGTH_SHORT).show();
                        return;
                    }			
                    ExchangeBean.DataBean.ListBean listBean = exchangeAdapter.list.get(selectposition);
                    //账户余额必须大于所选item 需要的金额
                    if (i >= listBean.getPrice()){
                        if (listBean.getStockCount()>0){
                            go2Exchange(listBean.getPrice());
                        }else{
                            Toast.makeText(this, "库存不足", Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        Toast.makeText(this, "余额不足", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(this, "两次手机号不同", Toast.LENGTH_SHORT).show();
                }
            }else{
                Toast.makeText(this, "手机号长度为11位", Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(this, "手机号为空", Toast.LENGTH_SHORT).show();
        }
    }
	
    private void go2Exchange(int price) {
        int a=i-price-2;
        Intent intent = new Intent(ExchangeActivity.this, SucceedActivity.class);
        intent.putExtra("a",a);
        Log.d(TAG, "go2Exchange: "+a);
        startActivity(intent);
    }
	
    private void ShowToa(String mag) {
        Toast.makeText(this, mag, Toast.LENGTH_SHORT).show();
    }
	
    @Override
    public void ok(ExchangeBean bean) {
        list = bean.getData().getList();
        Log.d(TAG, "ok: "+ list);
        exchangeAdapter.setList(list);
    }

    @Override
    public void no(String msg) {
        ShowToa(msg);
    }
}